/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.yoyo.fragment.reading;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xuexiang.yoyo.R;
import com.xuexiang.yoyo.core.BaseFragment;
import com.xuexiang.yoyo.databinding.FragmentReading1Binding;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;

/**
 * @author liuxiangfei
 * @since 2023-07-24 00:19
 */
@Page(anim = CoreAnim.none)
public class Reading1Fragment extends BaseFragment<FragmentReading1Binding> {
    private static final int[] COLORS = new int[]{0xff0099cc, 0xffff4444, 0xff669900, 0xffaa66cc, 0xffff8800};

    XUIGroupListView mNotConnectView;

    ScrollView scrollView;
    TextView tv01;
    @NonNull
    @Override
    protected FragmentReading1Binding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentReading1Binding.inflate(inflater, container, attachToRoot);
    }

    /**
     * @return 返回为 null意为不需要导航栏
     */
    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle().setTitle("学习");
        titleBar.addAction(
                new TitleBar.TextAction("设置"){

                    @Override
                    public void performAction(View view) {
                        XToastUtils.toast("点击测试");
                    }
                }
        );
        return titleBar;
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initViews() {
        mNotConnectView = findViewById(R.id.notConnectView);
        initNotConnectView();

        initEnableBt();

        scrollView = findViewById(R.id.ScrollView01);
        tv01 = findViewById(R.id.tv01);

        scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                scrollView.post(new Runnable() {
                    public void run() {
                        scrollView.fullScroll(View.FOCUS_DOWN);
                    }
                });
            }
        });


    }


    private void initEnableBt() {
        final EditText et_meter_coder = findViewById(R.id.et_meter_coder);
        final Button enableBt = findViewById(R.id.enableBt);
        enableBt.setOnClickListener(v -> {
            et_meter_coder.setEnabled(!et_meter_coder.isEnabled());
            enableBt.setText(et_meter_coder.isEnabled() ? "Read" : "Reading");
            if(!et_meter_coder.isEnabled()){

                mNotConnectView.setVisibility(View.VISIBLE);
            }


            String s = "%s\t\t%s\n";
            tv01.append(String.format(s,"Device ID 5 - Hardware verison","EM-135A 01.Y1.J0 02"));
            tv01.append(String.format(s,"Local Date","2012010504"));
            tv01.append(String.format(s,"Maximum Demand Import kW (last reset)","00000.000kW"));
            tv01.append(String.format(s,"Total Import Active Maximum Demand","11/30/02 12:00:00 AM"));
        });
    }

    private void initNotConnectView() {
        mNotConnectView.setVisibility(View.GONE);

        int size = DensityUtils.dp2px(getContext(), 20);


        XUIGroupListView.newSection(getContext()).setTitle("可连接通讯")
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addTo(mNotConnectView);
    }


}
